package club.kingyin.kyblogprocore.controller;



import club.kingyin.commons.constant.AuthorizeConstant;
import club.kingyin.commons.entity.core.Tag;
import club.kingyin.commons.response.Result;
import club.kingyin.easycache.annotation.EasyCache;
import club.kingyin.kyblogprocore.core.cache.IkWordsHandler;
import club.kingyin.kyblogprocore.dao.TagDao;
import club.kingyin.commons.entity.core.Documents;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import club.kingyin.kyblogprocore.service.TagService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Tag)表控制层
 *
 * @author makejava
 * @since 2022-02-16 18:22:14
 */
@RestController
@RequestMapping("tag")
public class TagController {
    /**
     * 服务对象
     */
    @Resource
    private TagService tagService;

    @Resource
    private TagDao tagDao;

//    /**
//     * 分页查询所有数据
//     *
//     * @param page 分页对象
//     * @param tag 查询实体
//     * @return 所有数据
//     */
//    @GetMapping
//    public Result selectAll(Page<Tag> page, Tag tag) {
//        return Result.success(this.tagService.page(page, new QueryWrapper<>(tag)));
//    }

    @EasyCache(module = "ky-core")
    @PostMapping("/ids")
    public Result getTagsByIds(@RequestBody Documents ids) {
        LambdaQueryWrapper<Tag> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(Tag::getUserid,ids.getIds()).select(Tag::getId,Tag::getName,Tag::getUserid);
        return Result.success(tagDao.selectList(queryWrapper));
    }

    @EasyCache(prams = {"userid"}, module = "ky-core")
    @GetMapping("/list")
    public Result tags(@RequestHeader(AuthorizeConstant.ID) int userid) {
        return Result.success(tagService.getByUserId(userid));
    }

    @EasyCache(prams = {"userid"}, module = "ky-core")
    @GetMapping("/list/user")
    public Result tagsByUserid(@RequestParam("userid") int userid) {
        return Result.success(tagService.getByUserId(userid));
    }

    @EasyCache(prams = {"userid"}, module = "ky-core")
    @GetMapping("/info")
    public Result infos(@RequestHeader(AuthorizeConstant.ID) int userid) {
        return Result.success(tagService.getTagsAndCountByUserId(userid));
    }

    @EasyCache(prams = {"name"}, handler = IkWordsHandler.class)
    @EasyCache(prams = {"userid"}, delete = {"tags","infos","tagsByUserid"}, module = "ky-core")
    @GetMapping("/add")
    public Result add(@RequestHeader(AuthorizeConstant.ID) int userid, @RequestParam("name") String name) {
        if (!tagService.containsName(userid,name)) {
            Tag tag = new Tag();
            tag.setUserid(userid);
            tag.setName(name);
            return tagService.save(tag)?Result.infoN("添加成功"):Result.failR("添加失败");
        }
        return Result.waringR(name+"已经存在了");
    }

    @EasyCache(prams = "tagId", handler = IkWordsHandler.class)
    @EasyCache(prams = {"userid"}, delete = {"tags","infos","tagsByUserid"}, module = "ky-core")
    @GetMapping("/delete")
    public Result delete(@RequestHeader(AuthorizeConstant.ID) int userid, @RequestParam("id") int tagId) {
        return tagService.removeTagFromDocumentByTagId(userid, tagId)?Result.infoN("删除成功"):Result.failR("删除失败");
    }

    @EasyCache(prams = {"userid"}, delete = {"tags","infos","tagsByUserid"}, module = "ky-core")
    @GetMapping("/update")
    public Result update(@RequestHeader(AuthorizeConstant.ID) int userid, @RequestParam("id") int tagId, @RequestParam("name") String name) {
        Tag tag = new Tag();
        tag.setId(tagId);
        tag.setUserid(userid);
        tag.setName(name);
        return tagService.updateById(tag)?Result.infoN("修改成功"):Result.failR("修改失败");
    }

//    /**
//     * 通过主键查询单条数据
//     *
//     * @param id 主键
//     * @return 单条数据
//     */
//    @GetMapping("{id}")
//    public Result selectOne(@PathVariable Serializable id) {
//        return Result.success(this.tagService.getById(id));
//    }
//
//    /**
//     * 新增数据
//     *
//     * @param tag 实体对象
//     * @return 新增结果
//     */
//    @PostMapping
//    public Result insert(@RequestBody Tag tag) {
//        return Result.success(this.tagService.save(tag));
//    }
//
//    /**
//     * 修改数据
//     *
//     * @param tag 实体对象
//     * @return 修改结果
//     */
//    @PutMapping
//    public Result update(@RequestBody Tag tag) {
//        return Result.success(this.tagService.updateById(tag));
//    }
//
//    /**
//     * 删除数据
//     *
//     * @param idList 主键结合
//     * @return 删除结果
//     */
//    @DeleteMapping
//    public Result delete(@RequestParam("idList") List<Long> idList) {
//        return Result.success(this.tagService.removeByIds(idList));
//    }
}

